<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog ba-operate-dialog-product-edit"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="750px" 
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                <span v-if="baTable.form.operate == 'Add'">{{ t('添加') }}</span>
                <span v-if="baTable.form.operate == 'Edit'">{{ t('编辑') }}</span>
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
            >
                
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-width="baTable.form.labelWidth + 'px'"
                    label-position="top"
                    :inline="false"
                >
                <FormItem :label="t('所属分类')" type="remoteSelect" style="width: calc(100% - 27px);" v-model="baTable.form.items!.category_id" :input-attr="{ class: 'category-select', pk: 'id', field: 'name_cn', 'remote-url': '/system/product.Category/index' }" />
                </el-form>

                    
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-width="baTable.form.labelWidth + 'px'"
                    label-position="top"
                    :inline="true"
                >
                    
                <FormItem :label="t('产品编码')" type="string" v-model="baTable.form.items!.code" />
                    <FormItem :label="t('产品名称')" type="string" v-model="baTable.form.items!.name_supplier" />
                    
                    
                    
            
                    
                    
            
                    <!-- <el-form-item :label="t('销售价格')">
                        <el-input
                            v-model="baTable.form.items!.sale_price"
                            style="width: 325px;"
                            class="input-with-select"
                            type="number"
                            >
                            <template #append>
                                <el-select v-model="baTable.form.items!.sale_currency" placeholder="Select" class="currency-select">
                                    <el-option label="CNY" value="CNY" />
                                    <el-option label="KRW" value="KRW" />
                                    <el-option label="USD" value="USD" />
                                </el-select>
                            </template>
                        </el-input>
                    </el-form-item>

                    <el-form-item :label="t('购买价格')">
                        <el-input
                            v-model="baTable.form.items!.buy_price"
                            style="width: 325px;"
                            class="input-with-select"
                            type="number"
                            >
                            <template #append>
                                <el-select v-model="baTable.form.items!.buy_currency" placeholder="Select" class="currency-select">
                                    <el-option label="CNY" value="CNY" />
                                    <el-option label="KRW" value="KRW" />
                                    <el-option label="USD" value="USD" />
                                </el-select>
                            </template>
                        </el-input>
                    </el-form-item> -->

                    
                
                    <FormItem :label="t('图纸文件')" type="files" v-model="baTable.form.items!.drawing" :input-attr="{ hideSelectFile: true, drag: true }" />
                    <FormItem :label="t('图纸图片')" type="images" v-model="baTable.form.items!.drawing_image" :input-attr="{ hideSelectFile: true, drag: true }" />

                    

                </el-form>
                        
                    <!-- 288px; -->
                    
                    
                    
                
            </div>
        </el-scrollbar>
        <template #footer>
            <div style="">
                <span class="btn" @click="baTable.toggleForm()">{{ t('取消') }}</span>
                <span class="btn btn-primary ml-12" v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('保存') }}
                </span>
            </div>
        </template>
    </el-dialog>

    <el-dialog v-model="state.dialogFormVisible" :title="t('报关元素')" width="500">
        <el-input v-model="state.element" type="textarea" rows="10" />
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="state.dialogFormVisible = false">{{ t('取消') }}</el-button>
                <el-button type="primary" @click="confirmElement">{{ t('确定') }}</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, inject, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance } from 'element-plus'


const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

import { getElementApi } from '/@/api/most/product/code'
import { each } from 'lodash-es'

const state = reactive({
    dialogFormVisible: false,
    element: ''
})

const blurHsCode = () => {
    let hs_code = baTable.form.items!.hs_code;
    if(hs_code == '' || hs_code == undefined) {
        baTable.form.items!.element = {};
        return;
    }

    getElementApi({ hs_code: hs_code, product_id: baTable.form.items!.id }).then((e) => {
        baTable.form.items!.element = e.data;
        if(e.data.length == 0){
            state.dialogFormVisible = true;
        }
    })

}

const confirmElement = () => {
    let sku_list = state.element.split('\n');  
        
    // 使用map方法去除每一行的空格，并排除空行  
    sku_list = sku_list.map(function(line) {  
        return line.trim();  
    }).filter(function(line) {  
        return line !== '';  
    });
    sku_list = new Set(sku_list);
    let element = [];
    sku_list.forEach(function(e){
        element.push({
            'name': e,
            'value': ''
        })
    })
    baTable.form.items!.element = element;
    state.dialogFormVisible = false;
}





</script>

<style scoped lang="scss">

:deep(.el-upload-dragger){
    padding: 10px 10px 0 10px;
}


:deep(.el-form--inline .el-input) {
    --el-input-width: 325px;
}

:deep(.ba-input-item-files .w100) {
    width: 325px!important;
}
:deep(.el-upload--picture-card){
    --el-upload-picture-card-size: 55px;
}
:deep(.el-upload-list--picture-card .el-upload-list__item){
    width: 55px;
    height: 55px;
}

:deep(.el-form--inline .category-select .el-input) {
    --el-input-width: 100%;
}
:deep(.el-form--inline .el-radio-group){
    width: 325px;
}
.el-form--inline .el-form-item:nth-child(even){
    margin-right: 0;
}
:deep(.currency-select .el-input){
    width: 100px!important;
}

</style>
